package com.smyhvae.animatordemo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.smyhvae.animatordemo.activity.ListViewActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView textView;
    private Button alphaButton, rotationButton, translationXButton, translationYButton,
            scaleXButton, scaleYButton, animatorSetButton, listViewButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        alphaButton.setOnClickListener(this);
        rotationButton.setOnClickListener(this);
        translationXButton.setOnClickListener(this);
        translationYButton.setOnClickListener(this);
        scaleXButton.setOnClickListener(this);
        scaleYButton.setOnClickListener(this);
        animatorSetButton.setOnClickListener(this);
        listViewButton.setOnClickListener(this);
    }

    private void initView(){
        textView = (TextView) findViewById(R.id.text);
        alphaButton = (Button) findViewById(R.id.alpha);
        rotationButton = (Button) findViewById(R.id.rotation);
        translationXButton = (Button) findViewById(R.id.translationX);
        translationYButton = (Button) findViewById(R.id.translationY);
        scaleXButton = (Button) findViewById(R.id.scaleX);
        scaleYButton = (Button) findViewById(R.id.scaleY);
        animatorSetButton = (Button) findViewById(R.id.animatorSet);
        listViewButton = (Button) findViewById(R.id.listView);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.alpha:
                ObjectAnimator alpha = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f, 1f);
                alpha.setDuration(5000);//设置属性动画的播放时间
                alpha.start();
                break;
            case R.id.rotation:
                ObjectAnimator rotation = ObjectAnimator.ofFloat(textView, "rotationX", 0f, 360f);
                rotation.setDuration(5000);
                rotation.start();
                break;
            case R.id.translationX:
                float curTranslationX = textView.getTranslationX();//获取当前TextViewDe的translationX的位置
                ObjectAnimator translationX = ObjectAnimator.ofFloat(textView, "translationX",
                        curTranslationX, -600f, curTranslationX);
                translationX.setDuration(5000);
                translationX.start();
                break;
            case R.id.translationY:
                float curTranslationY = textView.getTranslationY();//获取当前TextViewDe的translationY的位置
                ObjectAnimator translationY = ObjectAnimator.ofFloat(textView, "translationY",
                        curTranslationY, -600f, curTranslationY);
                translationY.setDuration(5000);
                translationY.start();
                break;
            case R.id.scaleX:
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(textView, "scaleX", 1f, 3f, 1f);
                scaleX.setDuration(5000);
                scaleX.start();
                break;
            case R.id.scaleY:
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(textView, "scaleY", 1f, 4f, 1f);
                scaleY.setDuration(5000);
                scaleY.start();
                break;
            case R.id.animatorSet:
                ObjectAnimator moveIn = ObjectAnimator.ofFloat(textView, "translationX", -600f, 0f);
                ObjectAnimator rotate = ObjectAnimator.ofFloat(textView, "rotation", 0f, 360f);
                ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f, 1f);
                AnimatorSet animSet = new AnimatorSet();
                animSet.play(rotate).with(fadeInOut).after(moveIn);
                animSet.setDuration(5000);
                animSet.start();
                break;
            case R.id.listView:
                Intent intent = new Intent(MainActivity.this, ListViewActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
